﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Collections;
using System.Drawing;

namespace WXYW
{
    public partial class RWGLDealWithForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TaskService service = new TaskService();
                Task task = service.GetTaskByTaskID(Convert.ToInt32(Request.QueryString["TID"]));
                if (!task.User1Reference.IsLoaded)
                {
                    task.User1Reference.Load();
                }
                if (!task.UserReference.IsLoaded)
                {
                    task.UserReference.Load();
                }
                subjectLabel.Text = task.Subject;
                createrLabel.Text = task.User1.ContactPerson;
                createdateLabel.Text = task.CreateDate.Value.ToString("yyyy-MM-dd");
                shouldLabel.Text = task.ShouldCompleteDate.Value.ToString("yyyy-MM-dd");                
                taskContentTxtBox.Text = task.Content;
                assigneeLabel.Text = task.User.ContactPerson;
                completeTextBox1.Text = DateTime.Now.ToString("yyyy-MM-dd");
                taskTypeLabel.Text = task.TaskType;
                taskQuestionType.Text = task.ReasonType;
                resultTextBox.Text = task.Result;
                if (!string.IsNullOrEmpty(task.ResultType))
                {
                    resultTypeDDL.SelectedValue = task.ResultType;
                }
                if (task.Status.Equals("待处理"))
                {
                    StatusRadioButtonList.SelectedValue = "处理中";
                }
                else
                {
                    StatusRadioButtonList.SelectedValue = task.Status;
                }
                if (!task.TaskFile.IsLoaded)
                {
                    task.TaskFile.Load();
                }
                if (task.TaskFile.Count() > 0)
                {
                    taskFileLnkBtn.ForeColor = Color.Gray;
                }
            }
        }

        protected void backLinkButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("RWGLTasksNeedDealWith.aspx");
        }

        protected void okLinkButton_Click(object sender, EventArgs e)
        {
            TaskService service = new TaskService();
            //service.DealWithTask(Convert.ToInt32(Request.QueryString["TID"]), resultTextBox.Text, completeTextBox1.Text);
            Task taskEntity = service.GetTaskByTaskID(Convert.ToInt32(Request.QueryString["TID"]));
            //taskEntity.CompleteDate = Convert.ToDateTime(completeTextBox1.Text);
            taskEntity.Result = resultTextBox.Text.Trim();
            taskEntity.Status = StatusRadioButtonList.SelectedValue;
            taskEntity.ResultType = resultTypeDDL.SelectedValue;
            service.Update(taskEntity);
            //ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('保存成功');", true);
            //Response.Redirect("RWGLTasksNeedDealWith.aspx");

            Response.Write(string.Format("<script language='JavaScript'> alert( '保存成功');location.href= 'RWGLTasksNeedDealWith.aspx' </script> ")); 
        }

        protected void finishForm_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(resultTextBox.Text))
            {
                Response.Write(string.Format("<script language='JavaScript'> alert( '请输入处理结果');location.href= 'RWGLDealWithForm.aspx?TID={0}' </script> ", Request.QueryString["TID"]));
            }
            else
            {
                TaskService service = new TaskService();
                //service.DealWithTask(Convert.ToInt32(Request.QueryString["TID"]), resultTextBox.Text, completeTextBox1.Text);
                Task taskEntity = service.GetTaskByTaskID(Convert.ToInt32(Request.QueryString["TID"]));
                taskEntity.Status = "已处理";
                taskEntity.CompleteDate = Convert.ToDateTime(completeTextBox1.Text);
                taskEntity.Result = resultTextBox.Text.Trim();
                taskEntity.ResultType = resultTypeDDL.SelectedValue;
                service.Update(taskEntity);
                //ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('处理完毕');", true);
                //Response.Redirect("RWGLTasksNeedDealWith.aspx");
                Response.Write(string.Format("<script language='JavaScript'> alert( '处理完毕');location.href= 'RWGLTasksNeedDealWith.aspx' </script> "));
            }
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<TaskFile> dds = new List<TaskFile>();
            dds.Add(new TaskFile());
            return dds;
        }

        protected void TaskFileGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
                ((HyperLink)e.Row.FindControl("ViewFileHyperLink")).NavigateUrl = string.Format("~/TaskFile/{0}_{1}", ((TaskFile)e.Row.DataItem).ID, ((TaskFile)e.Row.DataItem).FileName);
            }
        }

        protected void ModalPopupExtender1_Load(object sender, EventArgs e)
        {
            TaskService taskService = new TaskService();
            TaskFileGridView.DataSource = taskService.GetTaskFile(Convert.ToInt32(Request.QueryString["TID"])).ToList();
            TaskFileGridView.DataBind();
        }

        protected void uploadTaskFileBtn_Click(object sender, EventArgs e)
        {
            if (TaskFileFileUpload.HasFile)
            {
                TaskService taskService = new TaskService();
                long fileID = taskService.AddTaskFile(TaskFileFileUpload.FileName, Convert.ToInt32(Request.QueryString["TID"]));
                TaskFileFileUpload.SaveAs(Server.MapPath(string.Format("TaskFile/{0}_{1}", fileID, TaskFileFileUpload.FileName)));

                IQueryable<TaskFile>  files= taskService.GetTaskFile(Convert.ToInt32(Request.QueryString["TID"]));
                TaskFileGridView.DataSource = files.ToList();
                TaskFileGridView.DataBind();

                TaskFileModalPopupExtender.Show();                
            }
        }

        protected void taskFileLnkBtn_Click(object sender, EventArgs e)
        {
            TaskService taskService = new TaskService();
            TaskFileGridView.DataSource = taskService.GetTaskFile(Convert.ToInt32(Request.QueryString["TID"]));
            TaskFileGridView.DataBind();

            TaskFileModalPopupExtender.Show();
        }
    }
}
